package com.clouddms.repository;

import com.clouddms.entity.FileOperationLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface FileOperationLogRepository extends JpaRepository<FileOperationLog, Long> {

    /**
     * 根据用户ID查找文件操作日志
     * @param userId 用户ID
     * @return 文件操作日志列表
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file WHERE log.userId = :userId ORDER BY log.operationTime DESC")
    List<FileOperationLog> findByUserIdOrderByOperationTimeDesc(@Param("userId") Long userId);

    /**
     * 根据文件ID查找操作日志
     * @param fileId 文件ID
     * @return 文件操作日志列表
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file WHERE log.fileId = :fileId ORDER BY log.operationTime DESC")
    List<FileOperationLog> findByFileIdOrderByOperationTimeDesc(@Param("fileId") Long fileId);

    /**
     * 根据操作类型查找日志
     * @param operationType 操作类型
     * @return 文件操作日志列表
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file WHERE log.operationType = :operationType ORDER BY log.operationTime DESC")
    List<FileOperationLog> findByOperationTypeOrderByOperationTimeDesc(@Param("operationType") String operationType);

    /**
     * 根据时间范围查找日志
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 文件操作日志列表
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file WHERE log.operationTime BETWEEN :startTime AND :endTime ORDER BY log.operationTime DESC")
    List<FileOperationLog> findByOperationTimeBetweenOrderByOperationTimeDesc(
            @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 获取最近的文件操作日志
     * @return 最近的文件操作日志
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file ORDER BY log.operationTime DESC")
    List<FileOperationLog> findRecentLogs();

    /**
     * 根据用户ID获取最近的文件操作日志
     * @param userId 用户ID
     * @return 最近的文件操作日志
     */
    @Query("SELECT log FROM FileOperationLog log LEFT JOIN FETCH log.user LEFT JOIN FETCH log.file WHERE log.userId = :userId ORDER BY log.operationTime DESC")
    List<FileOperationLog> findRecentLogsByUserId(@Param("userId") Long userId);

    /**
     * 统计用户文件操作次数
     * @param userId 用户ID
     * @param operationType 操作类型
     * @return 操作次数
     */
    @Query("SELECT COUNT(log) FROM FileOperationLog log WHERE log.userId = :userId AND log.operationType = :operationType")
    long countByUserIdAndOperationType(@Param("userId") Long userId, @Param("operationType") String operationType);

    /**
     * 统计时间范围内的文件操作次数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 操作次数
     */
    @Query("SELECT COUNT(log) FROM FileOperationLog log WHERE log.operationTime BETWEEN :startTime AND :endTime")
    long countByOperationTimeBetween(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 统计文件操作类型分布
     * @return 操作类型和数量的映射
     */
    @Query("SELECT log.operationType, COUNT(log) FROM FileOperationLog log GROUP BY log.operationType")
    List<Object[]> countByOperationType();

    /**
     * 删除指定时间之前的日志
     * @param beforeTime 时间阈值
     * @return 删除的记录数
     */
    @Query("DELETE FROM FileOperationLog log WHERE log.operationTime < :beforeTime")
    int deleteByOperationTimeBefore(@Param("beforeTime") LocalDateTime beforeTime);

    /**
     * 获取热门文件（操作次数最多的文件）
     * @return 文件ID和操作次数的映射
     */
    @Query("SELECT log.fileId, COUNT(log) as operationCount FROM FileOperationLog log " +
           "WHERE log.fileId IS NOT NULL " +
           "GROUP BY log.fileId ORDER BY operationCount DESC")
    List<Object[]> findHotFiles();
}